import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Request,
} from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";
import { Roles } from "src/utils/decorators/roles.guard";
import { JwtPayload } from "src/utils/interfaces/jwt.interface";
import { CreateOrderArrayDto } from "./dto/create-order-array";
import { CreateOrderDto } from "./dto/create-order.dto";
import { UpdateOrderDto } from "./dto/update-order.dto";
import { OrderService } from "./order.service";

@Controller("order")
@ApiTags("订单")
@ApiBearerAuth()
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @Get("list")
  @ApiOperation({
    summary:
      "列出所有订单列表，如果是管理员，列出所有，如果不是，列出当前用户所在公司的所有",
  })
  list(@Request() req) {
    if (req.user.aud === "admin") {
      return this.orderService.findAll();
    } else {
      const companyId = req.user.company;
      return this.orderService.findByCompany(companyId);
    }
  }

  @Get("count")
  @ApiOperation({
    summary:
      "列出所有订单数量，如果是管理员，列出所有，如果不是，列出当前用户所在公司的订单数量",
  })
  count(@Request() req: { user: JwtPayload }) {
    const companyId = req.user.company;
    return this.orderService.count(companyId);
  }

  @Get("count/done")
  @ApiOperation({
    summary:
      "列出所有订单数量，如果是管理员，列出所有，如果不是，列出当前用户所在公司的订单数量",
  })
  countDone(@Request() req: { user: JwtPayload }) {
    const companyId = req.user.company;
    return this.orderService.countDone(companyId);
  }
  @Get("last")
  @ApiOperation({
    summary: "找出最后一个新增的订单",
  })
  findLast(@Request() req: { user: JwtPayload }) {
    const companyId = req.user.company;
    return this.orderService.findLast(companyId);
  }

  @Get("last/done")
  @ApiOperation({
    summary: "找出最后一个完成的订单",
  })
  findLastDone(@Request() req: { user: JwtPayload }) {
    const companyId = req.user.company;
    return this.orderService.findLastDone(companyId);
  }

  @Post()
  @ApiOperation({
    summary: "为当前公司创建订单",
  })
  create(
    @Request() req: { user: JwtPayload },
    @Body() createOrderDto: CreateOrderDto
  ) {
    const companyId = req.user.company;
    const userId = req.user.sub;
    return this.orderService.create(createOrderDto, userId, companyId);
  }

  @Post("insert")
  @ApiOperation({
    summary: "为当前公司批量创建订单",
  })
  insert(
    @Request() req: { user: JwtPayload },
    @Body() createOrderArrayDto: CreateOrderArrayDto
  ) {
    const companyId = req.user.company;
    const userId = req.user.sub;
    return this.orderService.insert(createOrderArrayDto, userId, companyId);
  }

  @Get("list/:companyId")
  @Roles("admin")
  @ApiOperation({
    summary: "列出公司id对应的所有订单列表, 只有管理员有此权限",
  })
  listByCompany(@Param("companyId") companyId: string) {
    return this.orderService.findByCompany(companyId);
  }

  @Get("client/:clientId")
  @ApiOperation({
    summary: "列出客户id对应的所有订单列表",
  })
  listByClient(@Param("clientId") clientId: string) {
    return this.orderService.findByClient(clientId);
  }

  @Put("done/:id")
  @ApiOperation({
    summary: "锁定指定ID的订单",
  })
  done(@Request() req: { user: JwtPayload }, @Param("id") id: string) {
    const userId = req.user.sub;
    return this.orderService.done(id, userId);
  }

  @Get(":id")
  @ApiOperation({
    summary: "根据ID查找订单",
  })
  find(@Param("id") id: string) {
    return this.orderService.find(id);
  }

  @Put(":id")
  @ApiOperation({
    summary: "更新指定ID的订单",
  })
  update(
    @Request() req: { user: JwtPayload },
    @Body() updateOrderDto: UpdateOrderDto,
    @Param("id") id: string
  ) {
    const userId = req.user.sub;
    return this.orderService.update(updateOrderDto, id, userId);
  }

  @Delete(":id")
  @ApiOperation({
    summary: "删除指定ID的订单",
  })
  remove(@Param("id") id: string) {
    return this.orderService.remove(id);
  }
}
